﻿/*
 *  Parameters.cs
 *
 *  Copyright (C) 2008 Dreamlusion <dreamlusion@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License version
 *      2 as published by the Free Software Foundation.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using phpMyAdminToolkit.Common;

namespace phpMyAdminToolkit.Dump
{
	class Parameters : ParametersBase
	{
		[Flags]
		enum ParametersIndex
		{
			None = 0,
			PmaUri = 1,
			Password = 2,
			User = 4,
			DatabaseName = 8,
			SqlCommand = 16,
			BackupDirectory = 32,
			IncludedTables = 64,
			ExcludedTables = 128,
			Usage = 256
		}

		public Parameters(string[] args)
			: base(args) { }

		ParametersIndex LoadedParams { get; set; }

		bool? _help;
		public bool Usage
		{
			get
			{
				if ((LoadedParams & ParametersIndex.Usage) != ParametersIndex.Usage)
				{
					LoadedParams |= ParametersIndex.Usage;
					try
					{
						_help = this.Count(kvp => kvp.Key == "?" || kvp.Key == "help" || kvp.Key == "h") > 0;
					}
					catch
					{
						// Program execution must report any errors.
					}
				}

				return _help.HasValue ? _help.Value : false;
			}
		}


		Uri _pmaUri;
		public Uri PmaUri
		{
			get
			{
				if ((LoadedParams & ParametersIndex.PmaUri) != ParametersIndex.PmaUri)
				{
					LoadedParams |= ParametersIndex.PmaUri;

					string pmaUri = string.Empty;

					try
					{
						_pmaUri = new Uri(this["pmaUri"]);
					}
					catch
					{
						// Empty.
					}
				}

				return _pmaUri;
			}
		}

		string _password;
		public string Password
		{
			get
			{
				if ((LoadedParams & ParametersIndex.Password) != ParametersIndex.Password)
				{
					LoadedParams |= ParametersIndex.Password;
					try
					{
						_password = this["password"];
					}
					catch
					{
					}
				}

				return _password;
			}
		}

		string _user;
		public string User
		{
			get
			{
				if ((LoadedParams & ParametersIndex.User) != ParametersIndex.User)
				{
					LoadedParams |= ParametersIndex.User;
					try
					{
						_user = this["user"];
					}
					catch
					{
					}
				}

				return _user;
			}
		}

		string _databaseName;
		public string DatabaseName
		{
			get
			{
				if ((LoadedParams & ParametersIndex.DatabaseName) != ParametersIndex.DatabaseName)
				{
					LoadedParams |= ParametersIndex.DatabaseName;
					try
					{
						_databaseName = this["databaseName"];
					}
					catch
					{
					}
				}

				return _databaseName;
			}
		}

		string _sqlCommand;
		public string SqlCommand
		{
			get
			{

				if ((LoadedParams & ParametersIndex.SqlCommand) != ParametersIndex.SqlCommand)
				{
					LoadedParams |= ParametersIndex.SqlCommand;
					try
					{
						_sqlCommand = this["sqlCommand"];
					}
					catch
					{
					}
				}

				return _sqlCommand;
			}
		}

		string _backupDirectory;
		public string BackupDirectory
		{
			get
			{
				if ((LoadedParams & ParametersIndex.BackupDirectory) != ParametersIndex.BackupDirectory)
				{
					LoadedParams |= ParametersIndex.BackupDirectory;
					try
					{
						_backupDirectory = this["backupDirectory"];
					}
					catch
					{
					}
				}

				return _backupDirectory;
			}
		}

		string _includedTables;
		/// <summary>
		/// Gets the table name patterns (as regular expressions) of the names of the included tables.
		/// </summary>
		public string IncludedTables
		{
			get
			{
				if ((LoadedParams & ParametersIndex.IncludedTables) != ParametersIndex.IncludedTables)
				{
					LoadedParams |= ParametersIndex.IncludedTables;
					try
					{
						_includedTables = this["includedTables"];
					}
					catch
					{
					}
				}

				return _includedTables;
			}
		}

		string _excludedTables;
		/// <summary>
		/// Gets the table name patterns (as regular expressions) of the names of the excluded tables.
		/// </summary>
		public string ExcludedTables
		{
			get
			{
				if ((LoadedParams & ParametersIndex.ExcludedTables) != ParametersIndex.ExcludedTables)
				{
					LoadedParams |= ParametersIndex.ExcludedTables;

					try
					{
						_excludedTables = this["excludedTables"];
					}
					catch
					{
					}
				}

				return _excludedTables;
			}
		}
	}
}
